package com.erb.utils;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
	/**
	 * 获取指定月的开始时间00:00:00
	 *
	 * @param month
	 * @return
	 */
	public static long getStartTimeOfMonth(int month) {
		Calendar calendar = Calendar.getInstance();

		calendar.setTimeInMillis(System.currentTimeMillis());
		calendar.set(Calendar.MONTH, month - 1);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND, calendar.getActualMinimum(Calendar.MILLISECOND));

		return calendar.getTimeInMillis();
	}

	/**
	 * 获取指定月的结束时间23:59:59
	 *
	 * @param month
	 * @return
	 */
	public static long getEndTimeOfMonth(int month) {
		Calendar calendar = Calendar.getInstance();

		calendar.setTimeInMillis(System.currentTimeMillis());
		calendar.set(Calendar.MONTH, month - 1);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));

		return calendar.getTimeInMillis();
	}

	/**
	 * 获取指定日期的开始时间
	 *
	 * @param date
	 * @return
	 */
	public static long getStartTimeOfDate(int date) {
		Calendar calendar = Calendar.getInstance();

		calendar.setTimeInMillis(System.currentTimeMillis());
		calendar.set(Calendar.DAY_OF_MONTH, date);
		calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND, calendar.getActualMinimum(Calendar.MILLISECOND));

		return calendar.getTimeInMillis();
	}

	/**
	 * 获取指定日期的结束时间
	 *
	 * @param date
	 * @return
	 */
	public static long getEndTimeOfDate(int date) {
		Calendar calendar = Calendar.getInstance();

		calendar.setTimeInMillis(System.currentTimeMillis());
		calendar.set(Calendar.DAY_OF_MONTH, date);
		calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));

		return calendar.getTimeInMillis();
	}

	public static void main(String[] args) {
		DateFormat dateTimeInstance = DateFormat.getDateTimeInstance();

		long startTimeOfDate = getStartTimeOfDate(1);
		long endTimeOfDate = getEndTimeOfDate(1);

		System.out.println(dateTimeInstance.format(new Date(startTimeOfDate)));
		System.out.println(dateTimeInstance.format(new Date(endTimeOfDate)));

		System.out.println(1000 * 60 * 60 * 24);
		System.out.println(endTimeOfDate - startTimeOfDate);
	}
}
